/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.sourceforge.bookscanwizard.config;

import java.awt.image.RenderedImage;
import java.text.DecimalFormat;
import javax.media.jai.Histogram;
import javax.media.jai.JAI;

/**
 * Calculates the autolevels for an image.
 */
public class ConfigAutoLevels {
    private DecimalFormat formatter = new DecimalFormat("#.#");

    public String getConfig(RenderedImage img) {
        StringBuilder retVal = new StringBuilder("Levels = ");
        Histogram histogram =
           (Histogram)JAI.create("histogram", img).getProperty("histogram");
        double[] blackLevels = histogram.getPTileThreshold(.01);
        double[] whiteLevels = histogram.getPTileThreshold(.99);
        for (int i=0; i < blackLevels.length; i++) {
            double level = blackLevels[i] * 100D / 255D;
            retVal.append(formatter.format(level)).append(",");
            level = whiteLevels[i] * 100D / 255D;
            retVal.append(formatter.format(level)).append(", ");
        }
        retVal.setLength(retVal.length() - 2);
        return retVal.toString();
    }
}
